<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ref获取子组件中的值和方法</title>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application">
        <child ref="childRef"></child>
    </div>
    <script>
        const App = Vue.createApp({
            setup() {

                const childRef = Vue.ref(null);

                Vue.onMounted(() => {
                    //dom加载后再获取
                    console.log("onMound后获取组件实例：", childRef.value); // child组件实例
                    console.log("onMound后获取组件的值：", childRef.value.message); // 组件的值
                    childRef.value.onChange();//执行子组件的方法
                });
                return {
                    childRef
                };
            },
        });

        //子组件
        const childBlack = {
            setup() {
                //一个信息
                const message = Vue.ref("我是子组件");
                //一个方法
                const onChange = () => {
                    alert("我是子组件方法")
                }
                return {
                    message,
                    onChange
                }

            },
            template: '<h1>{{message}}</h1>'
        }
        App.component("child", childBlack);

        //挂载
        App.mount("#Application");
    </script>
</body>

</html>